package com.yic.module.wms.controller.admin.itemwarehouse;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.api.factory.FactoryApi;
import com.yic.module.base.api.factory.dto.FactoryRespDTO;
import com.yic.module.base.api.item.ItemApi;
import com.yic.module.base.api.item.dto.ItemRespDTO;
import com.yic.module.wms.dal.dataobject.warehouse.WarehouseDO;
import com.yic.module.wms.dal.dataobject.warehousearea.WarehouseAreaDO;
import com.yic.module.wms.dal.dataobject.warehouselocation.WarehouseLocationDO;
import com.yic.module.wms.service.warehouse.WarehouseService;
import com.yic.module.wms.service.warehousearea.WarehouseAreaService;
import com.yic.module.wms.service.warehouselocation.WarehouseLocationService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.wms.controller.admin.itemwarehouse.vo.*;
import com.yic.module.wms.dal.dataobject.itemwarehouse.ItemWarehouseDO;
import com.yic.module.wms.convert.itemwarehouse.ItemWarehouseConvert;
import com.yic.module.wms.service.itemwarehouse.ItemWarehouseService;

@Tag(name = "管理后台 - 产品仓储信息")
@RestController
@RequestMapping("/wms/item-warehouse")
@Validated
public class ItemWarehouseController {

    @Resource
    private ItemWarehouseService itemWarehouseService;
    @Resource
    private FactoryApi factoryApi;
    @Resource
    private ItemApi itemApi;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private WarehouseAreaService warehouseAreaService;
    @Resource
    private WarehouseLocationService warehouseLocationService;

    @PostMapping("/create")
    @Operation(summary = "创建产品仓储信息")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse:create')")
    public CommonResult<Long> createItemWarehouse(@Valid @RequestBody ItemWarehouseCreateReqVO createReqVO) {
        return success(itemWarehouseService.createItemWarehouse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品仓储信息")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse:update')")
    public CommonResult<Boolean> updateItemWarehouse(@Valid @RequestBody ItemWarehouseUpdateReqVO updateReqVO) {
        itemWarehouseService.updateItemWarehouse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品仓储信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse:delete')")
    public CommonResult<Boolean> deleteItemWarehouse(@RequestParam("id") Long id) {
        itemWarehouseService.deleteItemWarehouse(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品仓储信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse:query')")
    public CommonResult<ItemWarehouseRespVO> getItemWarehouse(@RequestParam("id") Long id) {
        ItemWarehouseDO itemWarehouse = itemWarehouseService.getItemWarehouse(id);
        ItemWarehouseRespVO resp = ItemWarehouseConvert.INSTANCE.convert(itemWarehouse);
        if (ObjectUtil.isNotEmpty(resp)) {
            //封装产品
            ItemRespDTO item = itemApi.getItem(resp.getItemId());
            if (ObjectUtil.isNotEmpty(item)) {
                resp.setItemName(item.getName());
                resp.setItemCode(item.getCode());
                resp.setItemSpec(item.getSpec());
                resp.setDrawCode(item.getDrawCode());
            }
        }
        return success(resp);
    }

    @GetMapping("/get/{factoryId}/{itemId}")
    @Operation(summary = "获得产品仓储信息")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse:query')")
    public CommonResult<ItemWarehouseRespVO> getItemWarehouseByFactoryAndItem(@PathVariable("factoryId") Long factoryId, @PathVariable("itemId") Long itemId) {
        ItemWarehouseDO itemWarehouse = itemWarehouseService.getItemWarehouse(factoryId, itemId);
        ItemWarehouseRespVO resp = ItemWarehouseConvert.INSTANCE.convert(itemWarehouse);
        return success(resp);
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品仓储信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse:query')")
    public CommonResult<List<ItemWarehouseRespVO>> getItemWarehouseList(@RequestParam("ids") Collection<Long> ids) {
        List<ItemWarehouseDO> list = itemWarehouseService.getItemWarehouseList(ids);
        return success(ItemWarehouseConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品仓储信息分页")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse:query')")
    public CommonResult<PageResult<ItemWarehouseRespVO>> getItemWarehousePage(@Valid ItemWarehousePageReqVO pageVO) {
        PageResult<ItemWarehouseDO> pageResult = itemWarehouseService.getItemWarehousePage(pageVO);
        PageResult<ItemWarehouseRespVO> respVo = ItemWarehouseConvert.INSTANCE.convertPage(pageResult);
        respVo.getList().forEach(resp->{
            //封装工厂
            FactoryRespDTO factory = factoryApi.getFactory(resp.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                resp.setFactoryName(factory.getName());
            }
            //封装产品
            ItemRespDTO item = itemApi.getItem(resp.getItemId());
            if (ObjectUtil.isNotEmpty(item)) {
                resp.setItemName(item.getName());
                resp.setItemCode(item.getCode());
                resp.setItemSpec(item.getSpec());
                resp.setDrawCode(item.getDrawCode());
            }
            //封装仓库
            if (ObjectUtil.isNotEmpty(resp.getWarehouseId())) {
                WarehouseDO warehouse = warehouseService.getWarehouse(resp.getWarehouseId());
                if (ObjectUtil.isNotEmpty(warehouse)) {
                    resp.setWarehouseName(warehouse.getName());
                }
            }
            //封装库区
            if (ObjectUtil.isNotEmpty(resp.getAreaId())) {
                WarehouseAreaDO warehouseArea = warehouseAreaService.getWarehouseArea(resp.getAreaId());
                if (ObjectUtil.isNotEmpty(warehouseArea)) {
                    resp.setAreaName(warehouseArea.getName());
                }
            }
            //封装库位
            if (ObjectUtil.isNotEmpty(resp.getLocationId())) {
                WarehouseLocationDO warehouseLocation = warehouseLocationService.getWarehouseLocation(resp.getLocationId());
                if (ObjectUtil.isNotEmpty(warehouseLocation)) {
                    resp.setLocationName(warehouseLocation.getName());
                }
            }
        });
        return success(respVo);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品仓储信息 Excel")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse:export')")
    @OperateLog(type = EXPORT)
    public void exportItemWarehouseExcel(@Valid ItemWarehouseExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ItemWarehouseDO> list = itemWarehouseService.getItemWarehouseList(exportReqVO);
        // 导出 Excel
        List<ItemWarehouseExcelVO> datas = ItemWarehouseConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品仓储信息.xls", "数据", ItemWarehouseExcelVO.class, datas);
    }

}
